//
//  CEPViewController.m
//  YouthOlympic2014
//
//  Created by xiangpaopao on 14-6-26.
//  Copyright (c) 2014年 mitian. All rights reserved.
//

#import "CEPViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CEPCell.h"
#import "CEPDetailViewController.h"
#import "CepService.h"
#import "AppDelegate.h"

@interface CEPViewController ()

@property(nonatomic) NSInteger currentPage;
@property(nonatomic) float lastQuestionOffset;
@property(nonatomic, copy) NSArray* ceps;
@end

@implementation CEPViewController
@synthesize ceps;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:NSLocalizedString(@"CEP活动", nil)];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:self.HUD];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //
    if ([theApp isAppReachable]) {
        [self updateMainDataAndView];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
- (void)updateMainDataAndView
{
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.HUD show:YES];
    
    [[[CepService alloc] init] getAllCeps:^(id result, NSError *error) {
        [self.HUD hide:YES];

        if (nil == error) {
            self.ceps = result;
            [self.listView reloadData];
             NSLog(@"result is: %@",[AppUtils replaceUnicodeDic:result]);
        }else{
            //TODO
        }
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    return self.ceps.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CEPCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
        cell = [topLevelObjects objectAtIndex:0];
    }
    
    Cep* cep = self.ceps[indexPath.row];
    [cell.textLabel setText:cep.title];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Cep* cep = self.ceps[indexPath.row];
    
    self.HUD.mode = MBProgressHUDModeIndeterminate;
    [self.HUD show:YES];
    
    if ([theApp isAppReachable]) {
        CEPDetailViewController *detailViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"CEPDetailCtrl"];
        [[[CepService alloc] init] getCep:cep.id completion:^(id result, NSError *error) {
            [self.HUD hide:YES];
            if (nil == error) {
                Cep* cepdetail = result;
                [detailViewController initWithCEP: cepdetail];
                NSLog(@"cepdetail is: %@",[AppUtils replaceUnicodeDic:result]);
            }else{
                //TODO
            }
        }];
        [self.navigationController pushViewController:detailViewController animated:YES];
    }

}


//#pragma mark - collectionView delegate & datasource
//- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
//{
//    return self.ceps.count;
//}
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    CEPCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CEPCell" forIndexPath:indexPath];
//   
//    Cep* cep = self.ceps[indexPath.row];
//    cell.titleLabel.text = cep.title;
//    cell.subTitleLabel.text = cep.place;
//    cell.cepIconView.image = cep.icon;
//    cell.cepImgView.image = cep.image;
//
//    return cell;
//}
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    Cep* cep = self.ceps[indexPath.row];
//
//    self.HUD.mode = MBProgressHUDModeIndeterminate;
//    [self.HUD show:YES];
//    
//    if ([theApp isAppReachable]) {
//        CEPDetailViewController *detailViewController =  [self.storyboard instantiateViewControllerWithIdentifier:@"CEPDetailCtrl"];
//        [[[CepService alloc] init] getCep:cep.id completion:^(id result, NSError *error) {
//            [self.HUD hide:YES];
//            if (nil == error) {
//                Cep* cepdetail = result;
//                [detailViewController initWithCEP: cepdetail];
//                NSLog(@"cepdetail is: %@",[AppUtils replaceUnicodeDic:result]);
//            }else{
//                //TODO
//            }
//        }];
//        [self.navigationController pushViewController:detailViewController animated:YES];
//    }
//    
//}
//
//
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
//{
//    //实现吸附
//    if (self.lastQuestionOffset > scrollView.contentOffset.y)
//        self.currentPage = MAX(self.currentPage - 1, 0);
//    else if (self.lastQuestionOffset < scrollView.contentOffset.y)
//        self.currentPage = MIN(self.currentPage + 1, self.ceps.count-1);
//    
//    float questionOffset = 380 * self.currentPage;
//    self.lastQuestionOffset = questionOffset;
//    [self.listView setContentOffset:CGPointMake(0, questionOffset) animated:YES];
//}
//
//- (void)updateCEPDetail
//{
//    self.HUD.mode = MBProgressHUDModeIndeterminate;
//    [self.HUD show:YES];
//    
//    if ([theApp isAppReachable]) {
//        [[[CepService alloc] init] getAllCeps:^(id result, NSError *error) {
//            [self.HUD hide:YES];
//            if (nil == error) {
//                self.ceps = result;
//                [self.listView reloadData];
//            }else{
//                //TODO
//            }
//        }];
//    }
//    
//}
//

@end
